package com.jc.service.attributevalue;

import com.jc.cds.model.CustomAttributeValue;
import com.jc.common.util.CheckNullUtils;
import com.jc.ds.base.service.BaseServiceImpl;
import com.jc.ds.mapper.AttributeValueMapper;
import com.jc.ds.mapper.BaseCustomMapper;
import com.jc.ds.model.AttributeValue;
import com.jc.ds.model.ProAttribute;
import com.jc.service.productattribute.ProAttributeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class AttributeValueServiceImpl extends BaseServiceImpl<AttributeValue, CustomAttributeValue, Long> implements AttributeValueService {

    @Autowired
    private AttributeValueMapper attributeValueMapper;
    @Autowired
    private ProAttributeService proAttributeService;

    @Override
    public BaseCustomMapper<CustomAttributeValue, Long> getCustomMapper() {
        return null;
    }


    /***
     * getAttributeName
     * */
    @Override
    public Map<String, Object> queryAllName() {
       /* List<AttributeValue> attributeValues = this.attributeValueMapper.selectLikeAll(new AttributeValue() {{
            setAttributeName("");
        }});

        HashSet<String> attributeNames = new HashSet<>();
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> stringObjectHashMap = new HashMap<>();

        ArrayList<Object> attributeNameF = new ArrayList<>();
        attributeValues.forEach((attributeValue) -> {
            String attributeName = attributeValue.getAttributeName();
            attributeNames.add(attributeName);
        });
        int i = 1;
        for (String attributeName : attributeNames) {
            Map<String, Object> names = new HashMap<>();
            names.put("attributeName", attributeName);
            names.put("id", i);
            attributeNameF.add(names);
            i++;
        }

        result.put("data", attributeNameF);
        result.put("code", "10000");*/
        Map<String, Object> result = new HashMap<>();
        List<ProAttribute> proAttributes = this.proAttributeService.queryAll(new ProAttribute() {{
            setIsValid(1);
        }});
        result.put("data", proAttributes);
        result.put("code", 10000);
        result.put("msg", "成功");

        return result;
    }

    /**
     * getAttributeValue
     */
    @Override
    public Map<String, Object> getAttributeValue(Map<String, Object> params) {

        Map<String, Object> result = new HashMap<>();
        String attributeName = "";
        String attributeNameId = "";
        if (!CheckNullUtils.isNull(String.valueOf(params.get("attributeName")))) {
            attributeName = String.valueOf(params.get("attributeName"));
            String finalAttributeName = attributeName;
            List<AttributeValue> attributeValues = this.attributeValueMapper.selectAll(new AttributeValue() {{
                setAttributeName(finalAttributeName);
            }});
            result.put("data", attributeValues);
            result.put("code", "10000");
            result.put("msg", "成功");

        }
        if (!CheckNullUtils.isNull(String.valueOf(params.get("attributeNameId")))) {
            attributeNameId = String.valueOf(params.get("attributeNameId"));
            String finalAttributeNameId = attributeNameId;
            List<AttributeValue> attributeValues = this.attributeValueMapper.selectAll(new AttributeValue() {{
                setAttributeNameId(Long.valueOf(finalAttributeNameId));
            }});
            result.put("data", attributeValues);
            result.put("code", "10000");
            result.put("msg", "成功");

        }


        return result;
    }

    /**
     * 新增规格参数
     */
    @Transactional
    @Override
    public Map<String, Object> addAttribute(Map<String, Object> params) {

        Map<String, Object> result = new HashMap<>();

        String attributeName = String.valueOf(params.get("attributeName"));
        String attributeValue = String.valueOf(params.get("attributeValue"));
        if (CheckNullUtils.isNull(attributeValue)) {
            result.put("code", "5000");
            result.put("msg", "注意填写完整");
            return result;
        }

        List<AttributeValue> attributeValues = this.attributeValueMapper.selectAll(new AttributeValue() {{
            setAttributeName(attributeName);
            setAttributeValue(attributeValue);
        }});

        List<ProAttribute> proAttributes = this.proAttributeService.queryAll(new ProAttribute() {{
            setProductAttributeName(attributeName);
        }});

        int insert = 0;
        for (ProAttribute proAttribute : proAttributes) {
            if (CollectionUtils.isEmpty(attributeValues)) {
                insert = this.attributeValueMapper.insert(new AttributeValue() {{
                    setAttributeName(attributeName);
                    setAttributeValue(attributeValue);
                    setAttributeNameId(proAttribute.getId());
                }});
            } else {
                result.put("code", "5000");
                result.put("msg", "保存失败或者冲突了");
                return result;
            }
        }

        if (insert == 1) {
            result.put("code", "10000");
            result.put("data", "保存ok");
            return result;
        }

        result.put("code", "5000");
        result.put("msg", "保存失败或者冲突了");
        return result;
    }

    /**
     * 删除规格参数
     */
    @Transactional
    @Override
    public Map<String, Object> delAttribute(Map<String, Object> params) {

        String attributeName = String.valueOf(params.get("attributeName"));
        String attributeValue = String.valueOf(params.get("attributeValue"));
        Map<String, Object> resultMap = new HashMap<>();

        if ("null".equals(attributeName) || attributeName == null || "".equals(attributeName)) {
            resultMap.put("code", "7777");
            resultMap.put("msg", "参数一为空");
            return resultMap;
        }
        if ("null".equals(attributeValue) || attributeValue == null || "".equals(attributeValue)) {
            resultMap.put("code", "7777");
            resultMap.put("msg", "参数二为空");
            return resultMap;
        }

        List<AttributeValue> attributeValues = this.attributeValueMapper.selectAll(new AttributeValue() {{
            setAttributeName(attributeName);
            setAttributeValue(attributeValue);
        }});
        if (CollectionUtils.isEmpty(attributeValues)) {
            resultMap.put("code", "5000");
            resultMap.put("msg", "亲，该参数没了，关闭重新打开试试~");
            return resultMap;
        }
        ArrayList<Integer> integers = new ArrayList<>();
        for (AttributeValue value : attributeValues) {
            int i = this.attributeValueMapper.deleteByPrimaryKey(value.getId());
            integers.add(i);
        }
        if (integers.size() > 1) {
            resultMap.put("code", "5000");
            resultMap.put("msg", "该数值过多~~暂时无法删除");
            return resultMap;
        } else if (integers.size() == 0) {
            resultMap.put("code", "5000");
            resultMap.put("msg", "删除失败~~");
            return resultMap;
        }

        resultMap.put("code", "10000");
        resultMap.put("data", "删除成功");


        return resultMap;
    }
}
